package com.cmc.cloud.cmclink.doc.service.impl.so;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.SoBcrCheckErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.convert.SoBcrCheckConvert;
import com.cmc.cloud.cmclink.doc.entity.SoBcrCheckDO;
import com.cmc.cloud.cmclink.doc.mapper.SoBcrCheckMapper;
import com.cmc.cloud.cmclink.doc.service.so.SoBcrCheckService;
import com.cmc.cloud.cmclink.doc.vo.sovo.sobcrcheck.SoBcrCheckCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sobcrcheck.SoBcrCheckRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sobcrcheck.SoBcrCheckUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * SO的bcr检查 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class SoBcrCheckServiceImpl implements SoBcrCheckService {

    @Resource
    private SoBcrCheckMapper soBcrCheckMapper;

    @Override
    public void createSoBcrCheck(SoBcrCheckCreateReqVO createReqVO) {
        // 插入
        SoBcrCheckDO soBcrCheck = SoBcrCheckConvert.INSTANCE.convert(createReqVO);
        soBcrCheckMapper.insert(soBcrCheck);
    }

    @Override
    public void updateSoBcrCheck(SoBcrCheckUpdateReqVO updateReqVO) {
        // 校验存在
        validateSoBcrCheckExists(updateReqVO.getId());
        // 更新
        SoBcrCheckDO updateObj = SoBcrCheckConvert.INSTANCE.convert(updateReqVO);
        soBcrCheckMapper.updateById(updateObj);
    }

    @Override
    public void deleteSoBcrCheck(Long id) {
        // 校验存在
        validateSoBcrCheckExists(id);
        // 删除
        soBcrCheckMapper.deleteById(id);
    }

    private void validateSoBcrCheckExists(Long id) {
        if (soBcrCheckMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(SoBcrCheckErrorCodeConstants.SO_BCR_CHECK_NOT_EXISTS);
        }
    }

    @Override
    public SoBcrCheckDO getSoBcrCheck(Long id) {
        return soBcrCheckMapper.selectById(id);
    }

    @Override
    public List<SoBcrCheckDO> getSoBcrCheckList(Collection<Long> ids) {
        return soBcrCheckMapper.selectBatchIds(ids);
    }


    @Override
    public List<SoBcrCheckRespVO> insertBatch(List<SoBcrCheckCreateReqVO> createList) {
        if (CollectionUtil.isEmpty(createList)) {
            return new ArrayList<>();
        }
        // 插入
        List<SoBcrCheckDO> soBcrCheckList = SoBcrCheckConvert.INSTANCE.convertCreateList(createList);
        soBcrCheckMapper.insertBatch(soBcrCheckList);
        return SoBcrCheckConvert.INSTANCE.convertList(soBcrCheckList);
    }

    @Override
    public void deleteBySoId(Long soId) {
        if (soId != null) {
            soBcrCheckMapper.deleteBySoId(soId);
        }
    }

    @Override
    public void deleteByBusinessId(Long businessId) {
        if (businessId != null) {
            soBcrCheckMapper.deleteByBusinessId(businessId);
        }
    }

    @Override
    public List<SoBcrCheckRespVO> selectBySoId(Long soId) {
        if (soId != null) {
            LambdaQueryWrapperX<SoBcrCheckDO> queryWrapperX = new LambdaQueryWrapperX<SoBcrCheckDO>();
            queryWrapperX.eqIfPresent(SoBcrCheckDO::getSoId, soId);
            queryWrapperX.orderByDesc(SoBcrCheckDO::getId);
            return SoBcrCheckConvert.INSTANCE.convertList(soBcrCheckMapper.selectList(queryWrapperX));
        }
        return new ArrayList<>();
    }
}
